const express = require('express')
const router = express.Router()
const { request } = require('../utils')
const { channel, source } = require('novel-parser')
const urls = require('../constants/urls')
const { UPDATE_SOURCE } = require('novel-parser/lib/servies/channel')

const { SOURCES_LIST, ADD_SOURCES, GET_SOURCE, BATCH_ADD_SOURCES } = channel

router.get.apply(router, request(urls[SOURCES_LIST], async () => {
  return source[SOURCES_LIST]()
}))

router.post.apply(router, request(urls[ADD_SOURCES], async (req) => {
  return source[ADD_SOURCES](req.body)
}))

router.get.apply(router, request(urls[GET_SOURCE], async (req) => {
  return source[GET_SOURCE](req.query)
}))

router.post.apply(router, request(urls[BATCH_ADD_SOURCES], async (req) => {
  const { url } = req.body
  return source[BATCH_ADD_SOURCES](url)
}))

router.put.apply(router, request(urls[UPDATE_SOURCE], async (req) => {
  return source[UPDATE_SOURCE](req.body)
}))

module.exports = router
